;layui.define(["table", "form"], function (exports) {
	var tab = layui.table, $ = layui.$;
	// 初始化用户设置的列
	$.get(layui.setter.base +"admin/user/settings?site=/admin/order/search", function(data) {
		var cols = [ [
			{
                field: "merchName",
                width: 200,
                title: "用户名称",
                sort: !0
            }, {
                field: "merchCode",
                width: 90,
                title: "用户编号",
                sort: !0
            }, {
                field: "bizTypeShow",
                title: "交易类型",
                templet: "#buttonTpl",
                minWidth: 120,
                align: "center"
            }, {
                field: "orderNo",
                title: "订单号",
                minWidth: 90
            }, {
                field: "outOrderNo",
                title: "商城订单号",
                minWidth: 90
            }, {
                field: "amountFormat",
                title: "交易金额(元)",
                templet: "<div><span style='color:#1E9FFF;font-weight: bold;'>{{d.amountFormat}}</span></div>",
                align: "right",
                minWidth: 120
            }, {
                field: "merchFeeFormat",
                title: "手续费(元)",
                align: "right",
                minWidth: 120
            }, {
                field: "orderStatusShow",
                title: "交易状态",
                templet: function (d) {
                    if (d.orderStatus == "TS") return '<span class="layui-badge layui-bg-green">' + d.orderStatusShow + '</span>';
                    if (d.orderStatus == "TI") return '<span class="layui-badge layui-bg-blue">' + d.orderStatusShow + '</span>';
                    if (d.orderStatus == "TF") return '<span class="layui-badge">' + d.orderStatusShow + '</span>';
                    if (d.orderStatus == "CX") return '<span class="layui-badge layui-bg-orange">' + d.orderStatusShow + '</span>';
                    return '<span class="layui-badge layui-bg-cyan">' + d.orderStatusShow + '</span>';
                },
                minWidth: 100,
                align: "center"
            }, {
                field: "createTime",
                title: "创建时间",
                minWidth: 166
            }, {
                field: "payTime",
                title: "完成时间",
                minWidth: 166
            }, {
                title: "操作",
                minWidth: 200,
                align: "left",
                fixed: "right",
                toolbar: "#table-content-list"
            }
		] ];
		for(var i=0; i<data.length; i++){
			if(data[i].hide){
				for(var k=0; k<cols[0].length; k++){
					if(cols[0][k].field == data[i].field){
						cols[0][k].hide = data[i].hide;
					}
				}
			}
		}
		// 呈现列表
		tab.render({
			done: function (res, curr, count) {
				$("#totalinfo span.sumAmount").html(res.sumAmount);
				$("#totalinfo span.sumFee").html(res.sumFee);
				$("#totalinfo span.sumCount").html(res.sumCount);
			},
			elem: "#LAY-web-list",
			url: layui.setter.base + "admin/order/listJson",
			cols: cols,
			page: !0,
			limit: 10,
			limits: [10, 15, 20, 25, 30],
			toolbar: true,
			text: {none: '暂无相关数据'}
		});
		tab.on("tool(LAY-web-list)", function (row) {
			if ("view" === row.event) {
				parent.layer.open({
					type: 2,
					title: "订单详情",
					content: layui.setter.base + "admin/order/view?id=" + row.data.id,
					maxmin: !0,
					area: ["70%", "90%"],
					btn: ["关闭"]
				});
				return;
			}

			if ("query" === row.event) {
				layer.confirm("确定发起订单状态查询？", function (e) {
					// 请求查询接口
					layui.admin.req({
						url: layui.setter.base + "admin/order/queryOrderStatus?id=" + row.data.id + "&r=" + Math.random(),
						data: null,
						done: function (res) {
							layer.msg(res.msg, {
							offset: '15px',
							icon: 1,
							time: 1000
							}, function () {
							layui.table.reload('LAY-web-list'); //重载表格
							});
						}
					});
					// 关闭
					layer.close(e);
				});
				return;
			}

	        if ("cancleWithdraw" === row.event) {
	            layer.confirm("确定撤消此订单回滚金额吗？", function (e) {
	                // 请求撤消接口
	                layui.admin.req({
	                    url: layui.setter.base
	                        + "admin/order/cancel?id=" + row.data.id
	                        + "&r=" + Math.random(),
	                    data: null,
	                    done: function (res) {
	                        layer.msg(res.msg, {
	                            offset: '15px',
	                            icon: 1,
	                            time: 1000
	                        }, function () {
	                            console.log("重新加载执行了")
	                            layui.table.reload('LAY-web-list'); //重载表格
	                        });
	                    }
	                });
	                // 关闭
	                layer.close(e);
	            });
	            return;
	        }

	        if ("certificate" === row.event) {
	            parent.layer.open({
	                title: "查看凭证",
	                area: ['80%', '90%'],
	                content: "<p style='text-align:center'><img src='/admin/order/certificate?id=" + row.data.id + "'></p>",
	                btn: ['下载'],
	                yes: function () {
	                    window.open('/admin/order/certificate?id=' + row.data.id, "_blank");
	                }
	            });
	        }
		});
		// 将列的变更保存
		layui.onTabColsChange=function(cols){
			var arr = [];
			for(var i=0; i<cols.length; i++){
				var col = cols[i];
				!col.toolbar && arr.push({field : col.field, hide : col.hide});
			}
			layui.$.ajax('/admin/user/settings', {
				type : 'POST',
				contentType : "application/json;charset=utf-8",
				processData : false,
				data : JSON.stringify({
					site : '/admin/order/search',
					fields : arr
				})
			});
		};
		exports("order", {});
	});
});
